package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.asserts.GlobleAssert;
import cn.itsource.hrm.client.LoginUserClient;
import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.LoginUser;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.domain.TenantType;
import cn.itsource.hrm.dto.TenantEnteringDto;
import cn.itsource.hrm.exception.GlobleException;
import cn.itsource.hrm.mapper.EmployeeMapper;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.query.TenantQuery;
import cn.itsource.hrm.service.ITenantService;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2021-09-11
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;
    @Override
    public PageList<Tenant> selectPageList(TenantQuery query) {

        //分页条件
        Page<TenantType> page = new Page<TenantType>(query.getPage(),query.getRows());
        //查询条件
        List<Tenant> datas = tenantMapper.loadPageList(page,query);
        return  new PageList<>(page.getTotal(),datas);
    }

    @Autowired
    private LoginUserClient loginUserClient;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Transactional //本地事务其实没用，要用分布式事务
    @Override
    public void entering(TenantEnteringDto dto) {

        Employee admin = dto.getEmployee();
        Tenant tenant = dto.getTenant();

        //1参数校验 //@TODO 港仔来写
        //1) 非空检验
        /**
        GlobleAssert.hasText(dto.getTenant().getCompanyName() ,"公司名称不能为空" );
        GlobleAssert.hasText(dto.getTenant().getCompanyNum() ,"公司统一社会信用编码不能为空" );
        GlobleAssert.isMobile(dto.getTenant().getAdmin().getTel() ,"无效的手机" );
        GlobleAssert.isEmail(dto.getTenant().getAdmin().getEmail() ,"无效的邮箱" );
        **/

        //2) 是否能重校验-业务校验

        //t_tenant t_employee (其它服务-feign)t_login_user
        //2 远程保存  t_login_user 返回login_id
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(admin.getUsername());
        loginUser.setPassword(admin.getPassword());
        loginUser.setType(0); //0管理员 1 用户
        AjaxResult result = loginUserClient.addOrUpdate(loginUser);
        if (!result.isSuccess()){
            throw new RuntimeException("xxjfjfj");
        }
        Long loginId =Long.valueOf(result.getResultObj().toString()) ;
        //3 本地保存管理员
        admin.setLoginId(loginId);
        employeeMapper.insert(admin);

        //4 保存租户
        tenant.setAdminId(admin.getId());
        tenantMapper.insert(tenant);

        //5 保存租户和套餐中间表关系
        Long tenantId = tenant.getId();
        Long mealId = dto.getMealId();
        tenantMapper.saveTenantMeals(tenantId,mealId);

    }
}
